<template>
  <div class="login-container">
    <el-row :gutter="20">
      <el-col :span="12"
              :xs="0"></el-col>
      <el-col :span="12"
              :xs="24">
        <el-form class="login-panel"
                 ref="form"
                 :rules="rules"
                 :model="loginForm"
                 label-width="80px"
                 :inline="false">
          <h1>Hello</h1>
          <h2>欢迎来到heymar后台</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User"
                      v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock"
                      :suffix-icon="View"
                      clearable
                      type="password"
                      v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       style="width:100%"
                       @click="getLogin"
                       :loading="loading">登录</el-button>
          </el-form-item>
        </el-form>

      </el-col>
    </el-row>

  </div>
</template>

<script setup lang="ts">
import { User, Lock, View } from '@element-plus/icons-vue'
import useUserStore from '@/stores/modeles/user'
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import timeMsg from '@/utils/times'
import type { FormRules, FormInstance } from 'element-plus'
import type { typeData } from '@/api/user/type'
let userStore = useUserStore()
let loginForm = reactive({ username: '', password: '' })
let $router = useRouter()
let $route = useRoute()
let loading = ref(false)
let form:any = ref<FormInstance>()
async function getLogin() {
  await form.value.validate()
  loading.value = true
  try {
    await userStore.getLogin(loginForm)
    if($route.query.redirect) {
      $router.push({path:`${$route.query.redirect}`})
    }else {
      $router.push('/')
    }
    loading.value = false
    ElNotification({ type: 'success', message: '登陆成功', title: `HI,${timeMsg()}好` })
  } catch (error) {
    loading.value = false
    ElNotification({ type: 'error', message: `${error}` })
  }
}
const validatouUsername = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    return callback()
  } else {
    callback(new Error('长度不足五位'))
  }
  if (value.trim == '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}
const rules = reactive<FormRules<typeData>>({
  username: [{ trigger: 'change', validator: validatouUsername }],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change',
    },
  ],
})
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}
.login-panel {
  width: 80%;
  margin-top: 30vh;
  padding: 40px;
  background: url('@/assets/images/login_form.png') no-repeat;
  color: white;
  h1 {
    font-size: 40px;
  }
  h2 {
    font-size: 20px;
    margin: 20px 0;
  }
}
</style>